package com.lanju.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.lanju.dao.DepositSlipDao;
import com.lanju.entity.DepositSlip;
import com.lanju.service.DepositSlipService;
@Service
public class DepositSlipServiceImpl implements DepositSlipService{
	
	private static final Logger LOGGER = LoggerFactory.getLogger(DepositSlipServiceImpl.class);
	
	@Autowired
	DepositSlipDao depositSlipDao;

	@Override
    @Transactional(value="financialmarketingTM", rollbackFor = Exception.class)
	public Map<String, Object> insertDepositSlip(Map<String, Object> map) {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
			DepositSlip ds=new DepositSlip();
			ds.setHouseAddr(map.get("houseAddr")==null?"":map.get("houseAddr").toString());
			ds.setMoney(map.get("money")==null?0:Integer.valueOf(map.get("money").toString()));
			ds.setPayDate(sdf.parse(map.get("payDate").toString()));
			ds.setSlipId(UUID.randomUUID().toString());
			ds.setUserName(map.get("userName")==null?"": map.get("userName").toString());
			ds.setCreateTime(new Date());
			ds.setCreator(map.get("creator").toString());
			ds.setUserId( map.get("userId").toString());
			ds.setContractId(map.get("contractId")== null ?"":map.get("contractId").toString());
			ds.setBankName(map.get("bankName") == null ? "": map.get("bankName").toString());
			ds.setPayAccount(map.get("payAccount")==null ? "": map.get("payAccount").toString());
			ds.setCreateTime(new Date());
			depositSlipDao.insertDepositSlip(ds);
			result.put("success", true);
			LOGGER.info("成功添加押金单");
		} catch (Exception e) {
			e.printStackTrace();
			result.put("success", false);
		}
		return result;
	}

	@Override
	public Map<String, Object> queryDepositSlip(Map<String, Object> map) {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			result.put("list", depositSlipDao.queryDepositSlip(map));
			result.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			result.put("success", false);
		}
		return result;
	}

	@Override
	public Map<String, Object> queryDepotSliById(Map<String, Object> map) {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			result.put("list", depositSlipDao.queryDepotSliById(map));
			result.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			result.put("success", false);
		}
		return result;
	}

	@Override
	@Transactional(value="financialmarketingTM", rollbackFor = Exception.class)
	public Map<String, Object> delDepotSliById(Map<String, Object> map) {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			DepositSlip ds=depositSlipDao.queryDepotSliById(map);
			if(ds !=null)
				depositSlipDao.delDepotSliById(ds);
			result.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			result.put("success", false);
		}
		return result;
	}

	@Override
	@Transactional(value="financialmarketingTM", rollbackFor = Exception.class)
	public Map<String, Object> updateDepositSlip(Map<String, Object> map) {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			JSONObject json=JSONObject.parseObject(JSONObject.toJSONString(map));
			DepositSlip obj=json.toJavaObject(DepositSlip.class);
			depositSlipDao.updateDepositSlip(obj);
			result.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			result.put("success", false);
		}
		return result;
	}
	
	

}
